<?php

namespace app\admin\model;

use think\Model;

class Pack extends Model
{
    # 设置当前模型对应的完整数据表名称
    protected $name = 'users_resource_pack';

    /**
     * 礼包总数
     * @param array $where
     * @return int
     */
    public static function countPack($where = [])
    {
        return self::where($where)->where('end_time', '>', time())->count();
    }

    /**
     * 礼包列表
     * @param $where
     * @param $page
     * @param $pagesize
     * @return array
     */
    public static function getPackList($where, $page, $pagesize)
    {
        $data = self::alias('p')
            ->join('users u', 'p.uid = u.id', 'left')
            ->field('p.*,u.nickname,u.headimg,u.role,u.auth_city')
            ->where($where)
            ->where('end_time', '>', time())
            ->page($page, $pagesize)
            ->order('p.create_time', 'desc')
            ->select();

        return $data ? $data->toArray() : [];
    }

    /**
     * 礼包列表统计
     * @param $where
     * @return mixed
     */
    public static function countPackList($where)
    {
        return self::alias('p')
            ->join('users u', 'p.uid = u.id', 'left')
            ->where($where)
            ->where('end_time', '>', time())
            ->count();
    }

    /**
     * 本周礼包总数
     * @return int
     */
    public static function countPackWeek()
    {
        return self::whereWeek('create_time')->where('end_time', '>', time())->count();
    }

    /**
     * 本月礼包总数
     * @return int
     */
    public static function countPackMonth()
    {
        return self::whereMonth('create_time')->where('end_time', '>', time())->count();
    }

    /**
     * 社群礼包地区分布
     * @param int $offset
     * @param int $length
     * @return array
     */
    public static function getPackArea($offset = 0, $length = 6)
    {
        $data = self::fieldRaw("COUNT(*) nums,city as name")
            ->where('end_time', '>', time())
            ->group('city')
            ->order('nums', 'desc')
            ->limit($offset, $length)
            ->select();

        return $data ? $data->toArray() : [];
    }

    /**
     * 社群礼包地区统计
     * @return int
     */
    public static function countPackArea()
    {
        return self::where('end_time', '>', time())->count('distinct city');
    }

    /**
     * 社群礼包购买人
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getPackPeopleList()
    {
        $data = self::where('end_time', '>', time())->field('uid')->select();
        return $data ? $data->toArray() : [];
    }

    /**
     * 单社群礼包
     * @param $where
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function findPack($where)
    {
        $info = self::alias('p')
            ->join('users u', 'p.uid = u.id', 'left')
            ->field('p.*,u.nickname,u.headimg,u.role,u.auth_city,u.associate')
            ->where($where)
            ->find();
        return $info ? $info->toArray() : [];
    }
}